package interviewClassic150;

import org.junit.Test;

public class Rotate48 {
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n / 2; i++) {
            rotatePart(matrix,i);
        }
    }

    private void rotatePart(int[][] matrix, int start) {
        int n = matrix.length;
        int end = n - start;
        for (int i = 0; i < n - 2 * start - 1; i++) {
            //多轮移动
            int temp = matrix[start][start];
            //最左侧
            for (int j = start; j < end - 1; j++) {
                matrix[j][start] = matrix[j + 1][start];
            }
            //最下侧
            for (int j = start; j < end - 1; j++) {
                matrix[end - 1][j] = matrix[end - 1][j + 1];
            }
            //最右侧
            for (int j = end - 1; j >= start + 1; j--) {
                matrix[j][end - 1] = matrix[j - 1][end - 1];
            }
            //最上侧
            for (int j = end - 1; j >= start + 2; j--) {
                matrix[start][j] = matrix[start][j - 1];
            }
            matrix[start][start + 1] = temp;
        }
    }

    @Test
    public void test() {
        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
        rotate(matrix);
        System.out.println(matrix);
    }
}
